public class MySingleList {
    //创建节点类
    static class Node {
        public int val;
        public Node next;
        public Node(int val) {
            this.val = val;
            this.next = null;
        }
        public Node () {

        }
    }
    //创建链表类,包含关于操作链表的方法
    public static class LinkedList {
        private Node head;//头结点
        public LinkedList() {
            head = null;//带头空链表
        }

        //头插
        public void addFirst(int val) {
            Node newNode = new Node(val);
            newNode.next = head;
            head = newNode;
        }

        //尾插
        public void addLast(int val) {
            Node newNode = new Node(val);
            if(head == null) {
                head = newNode;
                return;
            }
            Node cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = newNode;
        }


        //获取链表的长度
        public int size() {
            int count = 0;
            Node cur = head;
            while (cur != null) {
                count++;
                cur = cur.next;
            }
            return count;
        }

        //打印链表
        public void print() {
            Node cur = head;
            while (cur != null) {
                System.out.print(cur.val + " ");
                cur = cur.next;
            }
        }


        //在第pos个位置插入节点
        public void addIndex(int pos, int val) {
            if (pos <= 0 || pos > size()+1) {
                throw new PosOutOfException("下标位置不合法");
            }
            if (pos == 1) {
                addFirst(val);
            }else if(pos == size()+1){
                addLast(val);
            }else {
                //在第pos个位置插入，cur需要走pos-2次，此时cur指向目标位置的前驱
                Node cur = head;
                for (int i = 0; i < pos - 2; i++) {
                    cur = cur.next;
                }
                Node newNode = new Node(val);
                newNode.next = cur.next;
                cur.next = newNode;
            }
        }

        //删除第pos个位置的结点
        public void remove(int pos) {
            if (pos <= 0 || pos > size() + 1) {
                throw new PosOutOfException("下标位置不合法");
            }
            if(pos == 1) {
                head = head.next;
                return;
            }
            //cur走pos-2后，cur指向目标位置的前驱
            Node cur = head;
            for (int i = 0; i < pos-2; i++) {
                cur = cur.next;
            }
            cur.next = cur.next.next;
        }

        //删除值为key的节点
        public void removeVal(int key) {
            if(head == null) {
                return;
            }
            if(head.val == key) {
                head = head.next;
                return;
            }
            Node ret = findLastNode(key);
            if(ret == null) {
                return;
            }
            ret.next = ret.next.next;
        }

        //找到值为key的前一个节点
        public Node findLastNode(int key) {
            if(head == null) return null;
            Node cur = head.next;
            Node pre = head;
            while(cur != null) {
                if(cur.val == key) {
                    return pre;
                }
                pre = cur;
                cur = cur.next;
            }
            return null;
        }


        //查找是否包含节点值为key的节点
        public boolean contains(int key) {
            if(head == null) {
                return false;
            }
            Node cur = head;
            while (cur != null) {
                if (cur.val == key) {
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }
    }
}
